﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using ChineseSchool.Models.Hethong;
using ChineseSchool.Models;
using ChineseSchool.BLL;

namespace ChineseSchool
{
    public partial class Home : System.Web.UI.MasterPage
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                lblProMenu.Text = LoadProMenu();
                lblTopmenu.Text = LoadMenuTop();
                lblMenu_Footer.Text = LoadMenuFooter();
                lblLoginbox.Text = LoadLoginbox();
            }
        }

        public string LoadProMenu()
        {
            string strMenu = "";
            DataTable dt = new DataTable();
            dt = new B_Menu().GetAll();
            if (dt != null && dt.Rows.Count > 0)
            {
                int MenuChaID = 0;
                int MenuTypeID = 2;
                List<M_Menu> lstMenu_cha = new B_Menu().GetBy_MenuParentID(MenuChaID, MenuTypeID);
                strMenu += "<ul class=\"pro_menu\">";
                for (int i = 0; i < lstMenu_cha.Count; i++)
                {
                    if (i == 0)
                    {
                        strMenu += "<li><a href=\"" + Util.MakeLinkMenuProInfo(lstMenu_cha[i].menuID.ToString(), Encode.Uni2KD(lstMenu_cha[i].menuName.ToString())) + "\"><img src=\"/images/pro_images/pro_home.png\" alt=\"" + lstMenu_cha[i].menuName + "\" title=\"" + lstMenu_cha[i].menuName + "\"/></a></li>";
                    }
                    else
                    {
                        int ID_Check = lstMenu_cha[i].menuID;
                        List<M_Menu> lstMenu_Check = (new B_Menu()).GetBy_MenuParentID(ID_Check, MenuTypeID);
                        if (lstMenu_Check != null && lstMenu_Check.Count > 0)
                        {
                            strMenu += "<li><a href=\"" + Util.MakeLinkMenuProInfo(lstMenu_cha[i].menuID.ToString(), Encode.Uni2KD(lstMenu_cha[i].menuName.ToString())) + "\">" + lstMenu_cha[i].menuName + "<span></span></a>";
                        }
                        else
                        {
                            strMenu += "<li><a href=\"" + Util.MakeLinkMenuProInfo(lstMenu_cha[i].menuID.ToString(), Encode.Uni2KD(lstMenu_cha[i].menuName.ToString())) + "\">" + lstMenu_cha[i].menuName + "</a>";
                        }
                    }
                    int MenuID = lstMenu_cha[i].menuID;
                    List<M_Menu> lstMenu_con = (new B_Menu()).GetBy_MenuParentID(MenuID, MenuTypeID);
                    if (lstMenu_con != null && lstMenu_con.Count > 0)
                    {
                        strMenu += "<ul>";
                        for (int j = 0; j < lstMenu_con.Count; j++)
                        {
                            int ID_Check_con = lstMenu_con[j].menuID;
                            List<M_Menu> lstMenu_Check_con = (new B_Menu()).GetBy_MenuParentID(ID_Check_con, MenuTypeID);
                            if (lstMenu_Check_con != null && lstMenu_Check_con.Count > 0)
                            {
                                strMenu += "<li><span></span><a href=\"" + Util.MakeLinkMenuProInfo(lstMenu_con[j].menuID.ToString(), Encode.Uni2KD(lstMenu_con[j].menuName.ToString())) + "\">" + lstMenu_con[j].menuName + "</a>";
                            }
                            else
                            {
                                strMenu += "<li><a href=\"" + Util.MakeLinkMenuProInfo(lstMenu_con[j].menuID.ToString(), Encode.Uni2KD(lstMenu_con[j].menuName.ToString())) + "\">" + lstMenu_con[j].menuName + "</a>";
                            }
                            int MenuID_Con = lstMenu_con[j].menuID;
                            List<M_Menu> lstMenu_con_next = (new B_Menu()).GetBy_MenuParentID(MenuID_Con, MenuTypeID);
                            if (lstMenu_con_next != null && lstMenu_con_next.Count > 0)
                            {
                                strMenu += "<ul>";
                                for (int k = 0; k < lstMenu_con_next.Count; k++)
                                {
                                    strMenu += "<li><a href=\"" + Util.MakeLinkMenuProInfo(lstMenu_con_next[k].menuID.ToString(), Encode.Uni2KD(lstMenu_con_next[k].menuName.ToString())) + "\">" + lstMenu_con_next[k].menuName + "</a>";
                                }
                                strMenu += "</li> ";
                                strMenu += "</ul> ";
                            }
                        }
                        strMenu += "</li> ";
                        strMenu += "</ul> ";
                    }
                    strMenu += "</li> ";
                }
                strMenu += "</ul> ";
            }
            return strMenu;
        }

        public string LoadMenuTop()
        {
            string strMenu = "";
            DataTable dt = new DataTable();
            dt = new B_Menu().GetAll();
            if (dt != null && dt.Rows.Count > 0)
            {
                int MenuChaID = 0;
                int MenuTypeID = 1;
                List<M_Menu> lstMenu_cha = new B_Menu().GetBy_MenuParentID(MenuChaID, MenuTypeID);
                strMenu += "<ul id=\"menu\" class=\"sf-menu\">";
                for (int i = 0; i < lstMenu_cha.Count; i++)
                {
                    if (i == 0)
                    {
                        strMenu += "<li  class=\"first current\"><a href=\"" + Util.MakeLinkMenuInfo(lstMenu_cha[i].menuID.ToString(), Encode.Uni2KD(lstMenu_cha[i].menuName.ToString())) + "\">" + lstMenu_cha[i].menuName + "</a>";
                    }
                    else
                    {
                        int ID_Check = lstMenu_cha[i].menuID;
                        List<M_Menu> lstMenu_Check = (new B_Menu()).GetBy_MenuParentID(ID_Check, MenuTypeID);
                        if (lstMenu_Check != null && lstMenu_Check.Count > 0)
                        {
                            strMenu += "<li><a href=\"" + Util.MakeLinkMenuInfo(lstMenu_cha[i].menuID.ToString(), Encode.Uni2KD(lstMenu_cha[i].menuName.ToString())) + "\">" + lstMenu_cha[i].menuName + "<span class=\"menu-arrow\"></span></a>";
                        }
                        else
                        {
                            strMenu += "<li><a href=\"" + Util.MakeLinkMenuInfo(lstMenu_cha[i].menuID.ToString(), Encode.Uni2KD(lstMenu_cha[i].menuName.ToString())) + "\">" + lstMenu_cha[i].menuName + "</a>";
                        }
                    }
                    int MenuID = lstMenu_cha[i].menuID;
                    List<M_Menu> lstMenu_con = (new B_Menu()).GetBy_MenuParentID(MenuID, MenuTypeID);
                    if (lstMenu_con != null && lstMenu_con.Count > 0)
                    {
                        strMenu += "<ul>";
                        for (int j = 0; j < lstMenu_con.Count; j++)
                        {
                            if (j == lstMenu_con.Count - 1)
                            {
                                strMenu += "<li class=\"bd_none\"><a href=\"" + Util.MakeLinkMenuInfo(lstMenu_con[j].menuID.ToString(), Encode.Uni2KD(lstMenu_con[j].menuName.ToString())) + "\">" + lstMenu_con[j].menuName + "</a>";
                            }
                            else
                            {
                                strMenu += "<li><a href=\"" + Util.MakeLinkMenuInfo(lstMenu_con[j].menuID.ToString(), Encode.Uni2KD(lstMenu_con[j].menuName.ToString())) + "\">" + lstMenu_con[j].menuName + "</a>";
                            }
                            int MenuID_Con = lstMenu_con[i].menuID;
                            List<M_Menu> lstMenu_con_next = (new B_Menu()).GetBy_MenuParentID(MenuID_Con, MenuTypeID);
                            if (lstMenu_con_next != null && lstMenu_con_next.Count > 0)
                            {
                                strMenu += "<ul>";
                                for (int k = 0; k < lstMenu_con_next.Count; k++)
                                {
                                    if (k == lstMenu_con_next.Count - 1)
                                    {
                                        strMenu += "<li class=\"bd_none\"><a href=\"" + Util.MakeLinkMenuInfo(lstMenu_con_next[k].menuID.ToString(), Encode.Uni2KD(lstMenu_con_next[k].menuName.ToString())) + "\">" + lstMenu_con_next[k].menuName + "</a>";
                                    }
                                    {
                                        strMenu += "<li ><a href=\"" + Util.MakeLinkMenuInfo(lstMenu_con_next[k].menuID.ToString(), Encode.Uni2KD(lstMenu_con_next[k].menuName.ToString())) + "\">" + lstMenu_con_next[k].menuName + "</a>";
                                    }
                                }
                                strMenu += "</li> ";
                                strMenu += "</ul> ";
                            }
                        }
                        strMenu += "</li> ";
                        strMenu += "</ul> ";
                    }
                    strMenu += "</li> ";
                }
                strMenu += "</ul> ";
            }
            return strMenu;
        }

        public string LoadMenuFooter()
        {
            string strMenu = "";
            DataTable dt = new DataTable();
            dt = new B_Menu().GetAll();
            if (dt != null && dt.Rows.Count > 0)
            {
                int MenuChaID = 0;
                int MenuTypeID = 1;
                List<M_Menu> lstMenu_cha = new B_Menu().GetBy_MenuParentID(MenuChaID, MenuTypeID);
                strMenu += "<ul id=\"menu_footer\" class=\"menu\">";
                for (int i = 0; i < lstMenu_cha.Count; i++)
                {
                    if (i == 0)
                    {
                        strMenu += "<li><a class=\"active\" href=\"" + Util.MakeLinkMenuInfo(lstMenu_cha[i].menuID.ToString(), Encode.Uni2KD(lstMenu_cha[i].menuName.ToString())) + "\">" + lstMenu_cha[i].menuName + "</a>";
                    }
                    else
                    {
                        strMenu += "<li><a href=\"" + Util.MakeLinkMenuInfo(lstMenu_cha[i].menuID.ToString(), Encode.Uni2KD(lstMenu_cha[i].menuName.ToString())) + "\">" + lstMenu_cha[i].menuName + "</a>";
                    }
                    strMenu += "</li> ";
                }
                strMenu += "</ul> ";
            }
            return strMenu;
        }

        public string LoadLoginbox()
        {
            string strLogbox = "";
            if (Session["Username"] != null && Session["Username"].ToString() != "" && Session["PassWord"] != null && Session["PassWord"].ToString() != "")
            {
                string username = Session["Username"].ToString();
                strLogbox += "<ul class=\"log-box\">";
                strLogbox += "<li class=\"first\"><a href=\"/tieng-trung/Tro-giup.html\">Trợ giúp</a></li>";
                strLogbox += "<li><a href=\"/Users.html?Username=" + username + "\">" + username + "</a></li>";
                strLogbox += "<li><a href=\"/Logout.html\">Thoát</a></li>";
                strLogbox += "</ul>";
            }
            else
            {
                strLogbox += "<ul class=\"log-box\">";
                strLogbox += "<li class=\"first\"><a href=\"/Register.html#toregister\">Đăng ký thành viên</a></li>";
                strLogbox += "<li><a href=\"/Login.html#tologin\">Đăng nhập</a></li>";
                strLogbox += "<li><a href=\"/tieng-trung/Tro-giup.html\">Trợ giúp</a></li>";
                strLogbox += "</ul>";
            }
            return strLogbox;
        }
    }
}